package com.isesol.ime.validator.param;

import com.isesol.arch.fluentvalidator.*;
import com.isesol.ime.dal.mapper.*;
import com.isesol.ime.dal.model.dto.*;
import com.isesol.ime.enums.*;
import org.apache.commons.collections.*;
import org.apache.commons.lang3.*;
import org.springframework.stereotype.*;

import javax.annotation.*;

@Component
public class ParamCreateValidator extends ValidatorHandler<ParamDTO> implements Validator<ParamDTO> {

	@Resource
	ParamMapper paramMapper;

	@Override
	public boolean validate(ValidatorContext context, ParamDTO paramDTO) {

		boolean result = true;

		if (StringUtils.isEmpty(paramDTO.getParamId())) {

			context.addErrorMsg("paramId is null");
			result = false;
		}

		if (StringUtils.isEmpty(paramDTO.getParamName())) {

			context.addErrorMsg("paramName is null");
			result = false;
		}

		if (null == paramDTO.getCategoryId()) {

			context.addErrorMsg("categoryId is null");
			result = false;

		}

		if (StringUtils.isEmpty(paramDTO.getName())) {

			context.addErrorMsg("name is null");
			result = false;
		}

		if (CollectionUtils.isNotEmpty(
				paramMapper.findByParamIdAndStatusAndParamVersion(paramDTO.getParamId(),
						StatusEnum.ON.getStatus(),
						paramDTO.getParamVersionId()))){

			context.addErrorMsg("paramId already exist,paramId = "+paramDTO.getParamId());
			result = false;
		}

		return result;
	}
}